package com.excilys.webservice.rest;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.excilys.converter.UserDTOToUserConverter;
import com.excilys.converter.UserToUserDTOConverter;
import com.excilys.dto.UserDTO;
import com.excilys.model.User;
import com.excilys.service.TransferFormService;
import com.excilys.service.UserService;

/**
 * Class that implements REST webservice persisting user and transferTransaction
 * 
 * @author excilys dev : SC, VP
 * 
 * 
 */

@Path("webservice/")
@Service("restWebService")
public class RestWebServiceImpl implements IRestWebService {

	@Autowired
	private UserService userService;

	@Autowired
	private UserToUserDTOConverter userConverterToDTO;

	@Autowired
	private UserDTOToUserConverter userConverterFromDTO;

	@Autowired
	private TransferFormService transactionTransferService;

	@GET
	@Produces("application/json")
	@Path("{login}")
	public UserDTO getUserByLogin(@PathParam("login") String login) {

		User user = userService.getUserByLogin(login);
		UserDTO userDto = userConverterToDTO.convert(user);

		return userDto;
	}

	@POST
	@Produces("application/json")
	@Path("persistoneuser/{userDTOFirstName}/{userDTOLastName}/{userDTOLogin}")
	@Override
	public void persistUserDTO(@PathParam("userDTOFirstName") String userDTOFirstName, @PathParam("userDTOLastName") String userDTOLastName,
			@PathParam("userDTOLogin") String userDTOLogin) {
		UserDTO userDTO = new UserDTO(userDTOFirstName, userDTOLastName, userDTOLogin, null);
		User user = userConverterFromDTO.convert(userDTO);
		userService.persistOneUser(user);
	}

	@POST
	@Produces("application/json")
	@Path("persistonetransaction/{sourceAccountName}/{targetAccountName}/{transferAmount}")
	@Override
	public void persistTransferTransaction(@PathParam("sourceAccountName") String sourceAccountName, @PathParam("targetAccountName") String targetAccountName,
			@PathParam("transferAmount") double transferAmount) {
		transactionTransferService.persistOneTransfer(sourceAccountName, targetAccountName, transferAmount);
	}
}
